<?php


namespace app\api\controller\v1;


use  app\api\service\Token;
use  app\api\service\Test as TestService;
use app\api\validate\SubjectAnalogParameter;
use app\lib\exception\SubjectException;
use app\lib\exception\SuccessMessage;

class Test
{
    /**
     *获取考试题目
     * @param $course
     * @param $car_status
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCourse($course,$car_status){
        $uid = Token::getCurrentUid();
        $srevice = new TestService();
        $result = $srevice->getExam($uid,$course,$car_status);
        return json_encode($result);
    }

    /**
     * 插入考试记录
     * @return false|int
     * @throws SubjectException
     * @throws \think\Exception
     */
    public function inserLog(){
        $data = input('post.');
        (new SubjectAnalogParameter())->goCheck();
        $uid = Token::getCurrentUid();
        $t_id = Token::getCurrentTokenVar('manager_id');
        $service = new TestService();
        $result =  $service->insertLog($uid,$t_id,$data);
        if (!$result){
            throw new SubjectException([
                "code"=>200,
                "msg"=>'提交失败',
                "error_code"=>100010
            ]);
        }
        return  json_encode(new SuccessMessage());
    }

    /**
     * 获取考试记录
     * @param $course
     * @return false|\PDOStatement|string|\think\Collection
     * @throws SubjectException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getLog($course,$car_status){
        $uid = Token::getCurrentUid();
        $service  = new TestService();
        $result =  $service->getUserLog($uid,$course,$car_status);
        if (!$result){
            throw new SubjectException([
                "code"=>200,
                "msg"=>'提交失败',
                "error_code"=>100010
            ]);
        }
        return $result;
    }

    /**
     * 获取用户排名
     * @param $course
     * @return false|\PDOStatement|string|\think\Collection
     * @throws SubjectException
     * @throws \app\lib\exception\TokenException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUserMaxRecode($course,$car_status){
        $uid = Token::getCurrentUid();
        $service  = new TestService();
        $result =  $service->getUserMaxRecode($uid,$course,$car_status);
        if (!$result){
            throw new SubjectException([
                "code"=>200,
                "msg"=>'没有最好成绩',
                "error_code"=>100010
            ]);
        }
        return json_encode($result);
    }

    /**
     * 获取用户排行
     * @param $course
     * @param $car_status
     * @return false|string
     * @throws SubjectException
     * @throws \app\lib\exception\TokenException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUserRank($course,$car_status){
        $t_id = Token::getCurrentTokenVar("manager_id");
        $uid = Token::getCurrentUid();
        $service  = new TestService();
        $result =  $service->getUserRank($uid,$t_id,$course,$car_status);
        if (!$result){
            throw new SubjectException([
                "code"=>200,
                "msg"=>'暂时还没有成绩排行',
                "error_code"=>100010
            ]);
        }
        return json_encode($result);
    }


    /**
     * 获取用户错题
     * @param $course
     * @param $car_status
     * @return false|\PDOStatement|string|\think\Collection
     * @throws SubjectException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getError($course,$car_status){
        $uid = Token::getCurrentUid();
        $service = new TestService();
        $result = $service->getError($uid,$course,$car_status);
        if (!$result){
            throw new SubjectException([
                "code"=>200,
                "msg"=>'该用户没有错题',
                "error_code"=>100030
            ]);
        }
        return $result;
    }

    /**
     * 删除错题
     * @param $false_id
     * @return int
     * @throws SubjectException
     */
    public function deleteError($false_id){
        $uid = Token::getCurrentUid();
        $service  = new TestService();
        $result = $service->deleteError($false_id);
        if (!$result){
            throw new SubjectException([
                "code"=>200,
                "msg"=>'该用户没有错题',
                "error_code"=>100040
            ]);
        }
        return  json_encode(new SuccessMessage());
    }

    /**
     * 获取视频信息
     * @return false|string
     */
    public function getVideoInfo(){
        //$uid = Token::getCurrentUid();
        $service  = new TestService();
         $result = $service->getVideoInfo();
         return json_encode($result);
    }



}